#!/usr/bin/env python

import datetime, threading, redis

interval = 60*60

def chkRedis(): 
	global interval 
	now=datetime.datetime.now()
	pool=redis.ConnectionPool(host='172.16.40.70', port=6379)
	r = redis.Redis(connection_pool=pool)
	l = r.llen('nums')
	str = '%d:%s\n' % (l,now)
	print(str)
	with open('redis-chk.txt', 'a') as f:
		f.write(str)

t = threading.Timer(interval, chkRedis)  
t.start()  


def createDaemon(stdin='/dev/null',stdout='/dev/null',stderr='/dev/null'):
	import os, sys, time
	sys.stdin = open(stdin,'r')
	sys.stdout = open(stdout,'a+')
	sys.stderr = open(stderr,'a+')
    #产生子进程，而后父进程退出
	try:
		pid = os.fork()
		if pid > 0:
			sys.exit(0)
	#os.chdir("/home/gaohang")
	#创建新的会话，子进程成为会话的首进程
	os.setsid()
	#修改工作目录的umask
	os.umask(0)
	#创建孙子进程，而后子进程退出
	try:
		pid = os.fork()
		if pid > 0:
			print("Daemon PID %d" % pid)
			sys.exit(0)

		chkRedis()
createDaemon()